# Contributor: Natanael Copa <ncopa@alpinelinux.org>
# Maintainer: Natanael Copa <ncopa@alpinelinux.org>
pkgname=snappy
pkgver=1.1.9
pkgrel=4
_googletest_commit=18f8200e3079b0e54fa00cb7ac55d4c39dcf6da6
pkgdesc="Fast compression and decompression library"
url="https://github.com/google/snappy"
arch="all"
license="BSD-3-Clause"
makedepends="cmake samurai"
subpackages="$pkgname-dbg $pkgname-static $pkgname-dev $pkgname-doc"
source="$pkgname-$pkgver.tar.gz::https://github.com/google/snappy/archive/$pkgver.tar.gz
	googletest-$_googletest_commit.tar.gz::https://github.com/google/googletest/archive/$_googletest_commit.tar.gz
	cmakelists.patch
	fix-inline.patch
	rtti.patch
	"

prepare() {
	rmdir third_party/googletest
	ln -s "$srcdir"/googletest-$_googletest_commit third_party/googletest
	default_prepare
}

build() {
	if [ "$CBUILD" != "$CHOST" ]; then
		CMAKE_CROSSOPTS="-DCMAKE_SYSTEM_NAME=Linux -DCMAKE_HOST_SYSTEM_NAME=Linux"
	fi
	cmake -B build -G Ninja \
		-DCMAKE_BUILD_TYPE=MinSizeRel \
		-DCMAKE_INSTALL_PREFIX=/usr \
		-DCMAKE_INSTALL_LIBDIR=/usr/lib \
		-DBUILD_SHARED_LIBS=ON \
		-DBUILD_STATIC_LIBS=ON \
		-DCMAKE_CXX_FLAGS="$CXXFLAGS" \
		-DCMAKE_C_FLAGS="$CFLAGS" \
		-DSNAPPY_BUILD_BENCHMARKS=OFF \
		-DSNAPPY_BUILD_TESTS=ON \
		$CMAKE_CROSSOPTS
	cmake --build build
}

check() {
	cd build
	CTEST_OUTPUT_ON_FAILURE=TRUE ctest
}

package() {
	DESTDIR="$pkgdir" cmake --install build

	# clean-up
	rm -r \
		"$pkgdir"/usr/include/gmock \
		"$pkgdir"/usr/include/gtest \
		"$pkgdir"/usr/lib/libgmock* \
		"$pkgdir"/usr/lib/libgtest* \
		"$pkgdir"/usr/lib/cmake/GTest \
		"$pkgdir"/usr/lib/pkgconfig

	local file; for file in AUTHORS CONTRIBUTING.md COPYING NEWS README.md \
		format_description.txt framing_format.txt; do
		install -m644 -D $file "$pkgdir"/usr/share/doc/$pkgname/$file
	done
}

sha512sums="
f1f8a90f5f7f23310423574b1d8c9acb84c66ea620f3999d1060395205e5760883476837aba02f0aa913af60819e34c625d8308c18a5d7a9c4e190f35968b024  snappy-1.1.9.tar.gz
f77c747218dc5ae4858b2b4c58a401eb895493adf470a331d6bac32cd20542533ff3a77081db653bbc18b9f55f6864be04e1739ef4afaed7db39d7c698abf880  googletest-18f8200e3079b0e54fa00cb7ac55d4c39dcf6da6.tar.gz
8a891edf2f0b267b05c67d54945f11d723b058ddab5ac3f27b4f00678759d4a52b617d0c5724c7170a285eb068ffa4ffbbc5a9efa250964979b3969fc8730eb3  cmakelists.patch
47aca85575dc53ed9cee127285e0545200d22c789a2bd8f03bb9a8cd93168b1b9b990c092d0b340122f800d2e4eafd0b718f04d09bc16ff4fe08cebb98daf741  fix-inline.patch
ec6a83d5e58deffd23f4b47e899b6645145548a823c2e1542812f05848a844839580a1b27aefa9beadba6b43f472abc38c42ea379db91da938837f4e69ae9cc0  rtti.patch
"
